/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.common.config;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import yhao.infra.common.YhaoInfraCommonProperties;
import yhao.infra.common.cache.YhaoCache;
import yhao.infra.common.cache.local.LocalCacheImpl;

import static yhao.infra.common.InfraCommonConstants.COMMON_CACHE_BEAN_NAME;

/**
 * 架构默认的缓存配置类
 *
 * @author yoara
 */
@ConditionalOnProperty(value = YhaoInfraCommonProperties.REDIS_ENABLE,havingValue = "false")
public class LocalCacheConfig {
    @Autowired
    private Environment env;

    @Bean(name = COMMON_CACHE_BEAN_NAME)
    public YhaoCache localCache() {
        YhaoCache localCache = new LocalCacheImpl();
        String appName = env.getProperty(YhaoInfraCommonProperties.ENV_APP_NAME);
        if(!StringUtils.isEmpty(appName)){
            localCache.setSystemUnique(appName.toUpperCase());
        }
        return localCache;
    }
}
